<template>
  <div class="manage">
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <!-- 表单 -->
      <el-form
        :inline="true"
        :rules="rules"
        ref="form"
        :model="form"
        label-width="80px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex">
            <el-option label="男" value="1"></el-option>
            <el-option label="女" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="birth">
          <el-date-picker
            v-model="form.birth"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-DD"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地址" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入地址"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <div class="manage-header">
      <!-- 点击弹出对话框 -->
      <el-button type="primary" @click="handleAdd"> + 新增</el-button>
      <!-- form搜索区域 -->
      <el-form :inline="true" :model="userForm">
        <el-form-item>
          <el-input placeholder="请输入名称" v-model="userForm.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-table">
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%" height="85%">
        <el-table-column prop="name" label="姓名"> </el-table-column>
        <el-table-column prop="age" label="年龄"> </el-table-column>
        <el-table-column prop="sex" label="性别">
          <template slot-scope="scope">
            <span>{{ scope.row.sex == "1" ? "男" : "女" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="birth" label="出生日期"> </el-table-column>
        <el-table-column prop="addr" label="地址"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 页码 -->
      <div class="page">
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          @current-change="handleChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { addUser, delUser, editUser, getUser } from "../api";
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        name: "",
        age: "",
        sex: "",
        birth: "",
        addr: "",
      },
      rules: {
        name: [{ required: true, message: "请输入名字" }],
        age: [{ required: true, message: "请输入年龄" }],
        sex: [{ required: true, message: "请选择性别" }],
        birth: [{ required: true, message: "请选择出生日期" }],
        addr: [{ required: true, message: "请输入地址" }],
      },
      tableData: [],
      modalType: 1, //1代表新增，0代表编辑
      total: 0, //数据的总量
      li: {
        page: 1,
        limit: 10,
      },
      userForm:{
        name:'',
      }
    };
  },
  methods: {
    // 确定按钮校验
    submit() {
      this.$refs.form.validate((val) => {
        // console.log(val,'val')
        if (val) {
          // 当值为true时，在进行后续
          console.log(this.form);
          // 当modalType为1时，说明时新增，就更新用户列表
          if (this.modalType === 1) {
            addUser(this.form).then(() => {
              this.userList();
            });
            // 否则为0就是编辑框
          } else {
            editUser(this.form).then(() => {
              this.userList();
            });
          }
          // 清空表单的值
          this.$refs.form.resetFields();
          // 关闭对话框
          this.dialogVisible = false;
        }
      });
    },
    // 点击关闭按钮时
    handleClose() {
      // 清空表单的值
      this.$refs.form.resetFields();
      // 关闭对话框
      this.dialogVisible = false;
    },
    // 取消按钮
    cancel() {
      this.handleClose();
    },
    // 编辑用户按钮
    handleEdit(row) {
      this.modalType = 0;
      this.form = row;
      this.dialogVisible = true;
    },
    // 删除用户按钮
    handleDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delUser({ id: row.id }).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
          this.userList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 增加用户按钮
    handleAdd() {
      this.modalType = 1;
      this.dialogVisible = true;
    },
    // 获取用户列表
    userList() {
      getUser({ params:{...this.userForm,...this.li}}).then((res) => {
        console.log(res);
        this.tableData = res.data.list;
        this.total = res.data.count;
      });
    },
    // 点击页码更新数据
    handleChange(val) {
      console.log(val);
      this.li.page = val;
      this.userList();
    },
    // 搜索
    search(){
      this.userList()
    }
  },
  mounted() {
    this.userList();
  },
};
</script>

<style lang="less" scoped>
.manage {
  height: 90%;
  .manage-header{
    display:flex;
    justify-content:space-between;
    align-items:center;
  }
  .common-table{
    position:relative;
    height:100%;
    .page{
      position:absolute;
      bottom:40px;
      right:20px;
    }
  }
}
</style>
